package practice1_100;

public class Practice605 {
    /**
     * 这题类似于贪心算法碰到1跳过，碰到0，看两边有没有1，没有就将0变成1
     * @param flowerbed
     * @param n
     * @return
     */
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        if(flowerbed.length==1){
            if(flowerbed[0]==0&&n<=1) return true;
            if(flowerbed[0]==1&&n<=0) return true;
            return false;
        }

        for (int i = 0; i < flowerbed.length; i++) {
            if(flowerbed[i]==1) continue;
            if(i==0&&flowerbed[i+1]!=1){
                n--;
                flowerbed[i]=1;
            }else if(i== flowerbed.length-1&&flowerbed[i-1]!=1){
                n--;
                flowerbed[i]=1;
            }else if(i-1>=0&&i+1<= flowerbed.length-1 &&flowerbed[i+1]!=1&&flowerbed[i-1]!=1){
                n--;
                flowerbed[i]=1;
            }
            if(n<=0) return true;
        }
        if(n<=0) return true;
        return false;
    }

    public static void main(String[] args) {
        boolean a= new Practice605().canPlaceFlowers(new int[]{1,0},1);
        System.out.println(a);
    }
}
